package com.kzd.gtgame

import android.os.Build
import android.text.method.LinkMovementMethod
import android.view.View
import android.webkit.*
import com.blankj.utilcode.util.ActivityUtils
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.callback.AppWebViewClient
import com.kzd.gtgame.custom.createViewEmptyNull
import com.kzd.gtgame.custom.createViewWeb
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.toAgreeLinks
import com.module.lemlin.owner.OwnerAbstractActivity
import kotlinx.android.synthetic.main.activity_agree.*

class AgreeActivity : OwnerAbstractActivity() {

    val mWebView: WebView by lazy {
        createViewWeb(context = this).apply {
            webViewClient = object : AppWebViewClient() {
                override fun onReceivedHttpError(
                    view: WebView?,
                    request: WebResourceRequest?,
                    errorResponse: WebResourceResponse?,
                ) {
                    super.onReceivedHttpError(view, request, errorResponse)
                    view?.loadUrl("about:blank")
                    toast("请检查当前网络设置")
                }

                override fun onReceivedError(
                    view: WebView?,
                    request: WebResourceRequest?,
                    error: WebResourceError?,
                ) {
                    super.onReceivedError(view, request, error)
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        view?.loadUrl("about:blank")
                        toast("请检查当前网络设置")
                    }
                }

                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
                    mSmartRefreshLayout.finishRefresh()
                    val isNotSuccess = url?.equals("about:blank") ?: false
                    mSmartRefreshLayout.setEnableRefresh(isNotSuccess)
                    cbAppAgreeSelect2.isEnabled = !isNotSuccess

                    if (isNotSuccess) {
                        if (hintRefreshLayout.windowToken == null) {
                            cbAppAgreeFrame.addView(hintRefreshLayout)
                        }
                    } else {
                        cbAppAgreeFrame.removeView(hintRefreshLayout)
                    }
                }

            }
        }
    }

    val hintRefreshLayout: View by lazy {
        createViewEmptyNull(this, color = R.color.white)
    }

    override val layoutResId: Int
        get() = R.layout.activity_agree

    override fun initView() {

        cbAppAgreeFrame.addView(mWebView)

        tvAppAgreeLinks.text = resources.getString(R.string.app_pact3).toAgreeLinks(activity = this)
        tvAppAgreeLinks.movementMethod = LinkMovementMethod.getInstance()

//        cbAppAgreeSelect1.setOnCheckedChangeListener { _, isChecked ->
//            tvAppAgreeSubmit.isEnabled = isChecked && cbAppAgreeSelect2.isChecked
//        }
        cbAppAgreeSelect2.isEnabled = false
        cbAppAgreeSelect2.setOnCheckedChangeListener { _, isChecked ->
//            tvAppAgreeSubmit.isEnabled = isChecked && cbAppAgreeSelect1.isChecked
            tvAppAgreeSubmit.isEnabled = isChecked
        }
        tvAppAgreeSubmit.setOnClickAntiShakeListener {
            SPSConstance.isReadAgree = true
            onBackPressed()
        }
        tvAppAgreeCancel.setOnClickAntiShakeListener {
            SPSConstance.isReadAgree = false
            onBackPressed()
        }

        mSmartRefreshLayout.setEnableLoadMore(false)

        mSmartRefreshLayout.setOnRefreshListener {
            mWebView.loadUrl("http://www.xianjuhy.com/?ct=wap&ac=agreement&type=yszc")
        }
    }

    override fun initData() {
        mWebView.loadUrl("http://www.xianjuhy.com/?ct=wap&ac=agreement&type=yszc")
    }

    override fun onRestart() {
        super.onRestart()
        if (SPSConstance.isReadAgree == true) {
            finish()
        }
    }

    override fun onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack()
        } else {
            if (SPSConstance.isReadAgree == true) {
                super.onBackPressed()
            } else {
                ActivityUtils.finishAllActivities()
            }
        }
    }


    override fun onDestroy() {
        if (mWebView.windowToken != null) {
            cbAppAgreeFrame.removeView(mWebView)
        }
        mWebView.clearHistory()
        mWebView.destroy()
        CookieManager.getInstance().removeAllCookies(null)
        CookieManager.getInstance().flush()
        super.onDestroy()
    }
}